package com.android.lovingpet.community.post;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;

import com.android.lovingpet.R;
import com.android.lovingpet.base.TakePhotoActivity;
import com.android.lovingpet.common.PopupPics;
import com.jph.takephoto.model.CropOptions;
import com.jph.takephoto.model.TImage;
import com.jph.takephoto.model.TResult;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class PostActivity extends TakePhotoActivity<PostContract.View, PostPresenter> implements PostContract.View, AlbumAdapter.Callback, PopupPics.OnPickListener {

    private EditText etContext;
    private AlbumAdapter adapter;
    private List<String> images;
    private int type;
    private PopupPics popupPics;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post);
        type = getIntent().getIntExtra("type", 0);
        popupPics = new PopupPics(this, this);
        etContext = findViewById(R.id.et_context);
        images = new ArrayList<>();
        images.add(null);
        adapter = new AlbumAdapter(images);
        adapter.setListener(this);
        RecyclerView recyclerView = findViewById(R.id.rv_album);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(adapter);
        findViewById(R.id.fab_post).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPresenter().post(etContext.getText().toString().trim(), images, type);
            }
        });
    }

    @Override
    public PostPresenter createPresenter() {
        return new PostPresenter();
    }

    @Override
    public PostContract.View createView() {
        return this;
    }

    @Override
    public void onDestroy() {
        popupPics = null;
        super.onDestroy();
    }

    @Override
    public void next() {
        Intent intent = new Intent();
        intent.putExtra("type", type);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void takeSuccess(TResult result) {
        List<TImage> resultImages = result.getImages();
        for (TImage tImage : resultImages) {
            images.add(images.size() - 1, tImage.getOriginalPath());
        }
        if (images.size() == 10) {
            images.remove(null);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onPick(View v) {
        popupPics.show(v);
    }

    @Override
    public void toDelete() {
        adapter.setShowDelete(true);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void notifyDataSetChanged(int position) {
        images.remove(position);
        boolean flag = !images.contains(null);
        if (flag) {
            images.add(null);
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyItemRemoved(position);
                adapter.notifyItemRangeChanged(position, images.size() - position);
                if (flag) {
                    adapter.notifyItemInserted(images.size() - 1);
                    adapter.notifyItemRangeChanged(images.size() - 1, 1);
                }
            }
        });
    }

    @Override
    public void fromGallery() {
        getTakePhoto().onPickMultiple(10 - images.size());
    }

    @Override
    public void fromCapture() {
        CropOptions cropOptions = new CropOptions.Builder().setWithOwnCrop(false).create();
        getTakePhoto().onPickFromCaptureWithCrop(getImageCropUri(), cropOptions);
    }
}
